<template>
  <div class="supplier-page">
    <!-- 搜索筛选区 -->
    <div class="filter-section">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="供应商编号:">
          <el-input v-model="searchForm.code" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="供应商名称:">
          <el-input v-model="searchForm.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="类型:">
          <el-select v-model="searchForm.type" placeholder="请选择" clearable>
            <el-option
              v-for="item in typeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="电话:">
          <el-input v-model="searchForm.phone" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="fetchData">
            <el-icon><Search /></el-icon>
            查询
          </el-button>
          <el-button @click="resetForm">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 数据列表区 -->
    <div class="data-list-section">
      <div class="list-header">
        <h3>数据列表</h3>
        <div class="action-buttons">
          <el-button type="primary" @click="onAdd">
            <el-icon><Plus /></el-icon>
            新增
          </el-button>
          <el-button @click="onEdit" :disabled="!selectedRows.length">
            <el-icon><Edit /></el-icon>
            编辑
          </el-button>
          <el-button @click="onDelete" :disabled="!selectedRows.length">
            <el-icon><Delete /></el-icon>
            删除
          </el-button>
          <el-button @click="fetchData">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
          <el-button @click="onExport">
            <el-icon><Upload /></el-icon>
            导出
          </el-button>
        </div>
      </div>

      <!-- 数据表格 -->
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        
      >
      <!-- @selection-change="onSelectionChange" -->
        <el-table-column type="selection" width="40" />
        <el-table-column prop="code" label="供应商编号" width="120" />
        <el-table-column prop="name" label="供应商名称" width="150" />
        <el-table-column prop="type" label="类型" width="120" />
        <el-table-column prop="phone" label="电话" width="130" />
        <el-table-column prop="fax" label="传真" width="120" />
        <el-table-column prop="email" label="Email" width="180" />
        <el-table-column prop="contact" label="联系人" width="100" />
        <el-table-column prop="address" label="地址" width="120" />
        <el-table-column prop="createdAt" label="创建时间" width="120" />
        <el-table-column label="操作" width="120">
          <template #default="scope">
            <el-button type="text" size="small" @click="onEdit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="onDelete([scope.row])">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 新增/编辑弹窗 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="700px"
      @close="resetFormData"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="供应商编号" prop="code">
              <el-input v-model="formData.code" placeholder="请输入供应商编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="name">
              <el-input v-model="formData.name" placeholder="请输入供应商名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-select v-model="formData.type" placeholder="请选择类型" style="width: 100%">
                <el-option
                  v-for="item in typeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="phone">
              <el-input v-model="formData.phone" placeholder="请输入电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="传真" prop="fax">
              <el-input v-model="formData.fax" placeholder="请输入传真" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="Email" prop="email">
              <el-input v-model="formData.email" placeholder="请输入Email" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="联系人" prop="contact">
              <el-input v-model="formData.contact" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input v-model="formData.address" placeholder="请输入地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="formData.remark"
            type="textarea"
            :rows="3"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus'
import { ElIcon } from 'element-plus'
import { Search, Refresh, Plus, Edit, Delete, Upload } from '@element-plus/icons-vue'

// 搜索表单
const searchForm = reactive({
  code: '',
  name: '',
  type: '',
  phone: ''
})

// 类型选项
const typeOptions = ref([
  { label: '合作供应商', value: '合作供应商' },
  { label: '原材料供应商', value: '原材料供应商' },
  { label: '设备供应商', value: '设备供应商' },
  { label: '服务供应商', value: '服务供应商' }
])

// 表格数据
const tableData = ref([
  {
    id: 1,
    code: 'GX0005',
    name: '中国电信',
    type: '合作供应商',
    phone: '15268945864',
    fax: '992682',
    email: '178499@qq.com',
    contact: '李浩',
    address: '北京',
    createdAt: '2019-05-29',
    remark: ''
  },
  {
    id: 2,
    code: 'GX0004',
    name: '中国联通',
    type: '合作供应商',
    phone: '05694482',
    fax: '05694482',
    email: '178499@qq.com',
    contact: '王聪',
    address: '北京',
    createdAt: '2019-05-29',
    remark: ''
  },
  {
    id: 3,
    code: 'GX0003',
    name: '华为科技',
    type: '合作供应商',
    phone: '15268945864',
    fax: '992682',
    email: '178499@qq.com',
    contact: '王聪',
    address: '北京',
    createdAt: '2019-05-29',
    remark: ''
  },
  {
    id: 4,
    code: 'GX0003',
    name: '华为科技',
    type: '合作供应商',
    phone: '15268945864',
    fax: '992682',
    email: '178499@qq.com',
    contact: '王聪',
    address: '北京',
    createdAt: '2019-05-29',
    remark: ''
  },
  {
    id: 5,
    code: 'GX0003',
    name: '华为科技',
    type: '合作供应商',
    phone: '15268945864',
    fax: '992682',
    email: '178499@qq.com',
    contact: '王聪',
    address: '北京',
    createdAt: '2019-05-29',
    remark: ''
  }
])

// 选中的行
const selectedRows = ref([])

// 弹窗相关
const dialogVisible = ref(false)
const dialogTitle = ref('新增供应商')
const isEdit = ref(false)
const formRef = ref<FormInstance>()

// 表单数据
const formData = reactive({
  id: '',
  code: '',
  name: '',
  type: '',
  phone: '',
  fax: '',
  email: '',
  contact: '',
  address: '',
  remark: ''
})

// 表单验证规则
const formRules: FormRules = {
  code: [
    { required: true, message: '请输入供应商编号', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入供应商名称', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择类型', trigger: 'change' }
  ],
  phone: [
    { required: true, message: '请输入电话', trigger: 'blur' }
  ],
  email: [
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ]
}

// 获取数据
const fetchData = () => {
  // TODO: 调用接口获取数据
  ElMessage.success('查询成功')
}

// 重置搜索表单
const resetForm = () => {
  searchForm.code = ''
  searchForm.name = ''
  searchForm.type = ''
  searchForm.phone = ''
  fetchData()
}

// // 选择变化
// const onSelectionChange = (val: any[]) => {
//   selectedRows.value = val
// }

// 新增
const onAdd = () => {
  dialogTitle.value = '新增供应商'
  isEdit.value = false
  dialogVisible.value = true
}

// 编辑
const onEdit = (row?: any) => {
  if (row) {
    // 单行编辑
    dialogTitle.value = '编辑供应商'
    isEdit.value = true
    Object.assign(formData, row)
    dialogVisible.value = true
  } else if (selectedRows.value.length === 1) {
    // 批量编辑（选中一行）
    dialogTitle.value = '编辑供应商'
    isEdit.value = true
    Object.assign(formData, selectedRows.value[0])
    dialogVisible.value = true
  } else {
    ElMessage.warning('请选择一条数据进行编辑')
  }
}

// 删除
const onDelete = (rows?: any[]) => {
  const toDelete = rows || selectedRows.value
  if (!toDelete.length) {
    ElMessage.warning('请选择要删除的数据')
    return
  }
  
  ElMessageBox.confirm(
    `确定要删除选中的 ${toDelete.length} 条数据吗？`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // TODO: 调用删除接口
    ElMessage.success('删除成功')
    fetchData()
  })
}

// 导出
const onExport = () => {
  ElMessage.info('导出功能待实现')
}

// 提交表单
const submitForm = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate((valid, fields) => {
    if (valid) {
      // TODO: 调用新增或编辑接口
      if (isEdit.value) {
        ElMessage.success('编辑成功')
      } else {
        ElMessage.success('新增成功')
      }
      dialogVisible.value = false
      fetchData()
    } else {
      console.log('error submit!', fields)
    }
  })
}

// 重置表单数据
const resetFormData = () => {
  if (formRef.value) {
    formRef.value.resetFields()
  }
  Object.assign(formData, {
    id: '',
    code: '',
    name: '',
    type: '',
    phone: '',
    fax: '',
    email: '',
    contact: '',
    address: '',
    remark: ''
  })
}

// 页面加载时获取数据
onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.supplier-page {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.filter-section {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.search-form {
  margin: 0;
}

.data-list-section {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.list-header h3 {
  margin: 0;
  color: #333;
  font-size: 16px;
  font-weight: 600;
}

.action-buttons {
  display: flex;
  gap: 10px;
}

.action-buttons .el-button {
  display: flex;
  align-items: center;
  gap: 4px;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table th) {
  background-color: #fafafa;
  color: #333;
  font-weight: 600;
}

:deep(.el-button--text) {
  padding: 0;
  margin: 0 8px;
}

:deep(.el-button--text:first-child) {
  margin-left: 0;
}

:deep(.el-form-item) {
  margin-bottom: 18px;
}

:deep(.el-dialog__body) {
  padding: 20px;
}
</style>
